var express = require('express');
var router = express.Router();
const { User, sequelize } = require('../../models');

const { success, failure } = require('../../utils/responses');

// 获取 性别数量
router.get('/sex', async function (req, res, next) {
    try {
        const male = await User.count({ where: { sex: 1 } });
        const famale = await User.count({ where: { sex: 0 } });
        const unKnow = await User.count({ where: { sex: 2 } });

        const data = [
            { value: male, name: '男性' },
            { value: famale, name: '女性' },
            { value: unKnow, name: '未知' },
        ]

        success(
            res,
            '查询用户性别成功',
            {data}
        )
    } catch (error) {
        failure(res, error)
    }
});

// 统计每个月用户数量
router.get('/user', async function (req, res, next) {
    try {
        const [results] = await sequelize.query(
            "SELECT DATE_FORMAT(`createdAt`, '%Y-%m') AS `month`, COUNT(*) AS `value` FROM `Users` GROUP BY `month` ORDER BY `month` ASC;"
        )

        const data = {
            months: [],
            values: []
        }
        results.forEach(item => {
            data.months.push(item.month);
            data.values.push(item.value)
        })
        success(
            res,
            '查询每月用户数量成功',
            {data}
        )
    } catch (error) {
        failure(res, error)
    }
});



module.exports = router;
